package com.hygj.baseUtil;

import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;

import com.hygj.util.PageUtil;

public class BaseServiceImpl<T> implements BaseService<T> {
	// 日志打印器
	// protected final static Logger LOGGER = LoggerFactory
	// .getLogger(BaseService.class);
	protected BaseDao<T> dao;
	private Class<T> clazz;

	// ==================================================
	public BaseDao<T> getDao() {
		return dao;
	}

	@Resource
	public void setDao(BaseDao<T> dao) {
		this.dao = dao;
	}

	public Class<T> getClazz() {
		return clazz;
	}

	public void setClazz(Class<T> clazz) {
		this.clazz = clazz;
	}

	// ==================================================
	@SuppressWarnings("unchecked")
	public BaseServiceImpl() {

		ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
		clazz = (Class<T>) type.getActualTypeArguments()[0];

	}

	public BaseServiceImpl(BaseDao<T> dao, Class<T> clazz) {
		super();
		this.dao = dao;
		this.clazz = clazz;
	}

	// ===================================================
	@Override
	public int insert(String sqlId, T t) {
		return this.dao.insert("insert", t);
	}
	
	@Override
	public int insert(T t) {
		return insert("insert", t);
	}

	@Override
	public int delete(String sqlId, Integer id) {
		return this.dao.delete("delete", id);
	}

	@Override
	public int update(String sqlId, T t) {
		return this.dao.update("update", t);
	}

	@Override
	public T getById(String sqlId, Integer id) {
		return this.dao.getById("getById", id);
	}

	@Override
	public List<T> listAll(String sqlId, Map<String, Object> params) {
		return dao.listAll(sqlId, params);
	}

	@Override
	public int pageCount(Map<String, Object> params) {
		return (int) this.dao.find("pageCount", params);
	}

	@Override
	public List<T> pageList(Map<String, Object> params) {
		return this.dao.listAll("page", params);
	}

	@Override
	public PageUtil<T> page(HttpServletRequest request, int pageSize, Map<String, Object> params) {
		// 获取总条数，先通过pageCount查询。
		int totalCount = this.pageCount(params);
		// totalCount总条数 pageSize每页显示多少条
		PageUtil<T> page = new PageUtil<T>(totalCount, pageSize);
		page.setRequest(request);
		page.show();
		params.put("pageOffset", page.getFirstRow());
		params.put("pageSize", pageSize);

		List<T> list = this.pageList(params);
		page.setLists(list);
		return page;
	}

	@Override
	public Object find(String sqlId, Map<String, Object> params) {
		return this.dao.find(sqlId, params);
	}

	
}
